/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.model;

import java.util.Date;
import java.util.List;

import org.geoscheduler.commons.marshaller.JSONField;

/**
 * Events.
 * 
 * @author paja
 * @author lko
 * @version $Id: Event.java 161 2008-04-09 19:02:45Z srakyi $
 */
public class Event extends VersionedEntity implements Identifiable, RemoteIdentifiable {

	public enum EventType {
		PRIVATE, PUBLIC, TEMPORARY;
	}

	public enum AlertType {
		LOCATION, TIME, LOCATION_TIME, LOCATION_OR_TIME, LOCATION_XOR_TIME, TAG, TAG_AND_TIME, TAG_OR_TIME, NONE
	}

	public enum RepeatType {
		ONCE, DAILY, WEEKLY, MONTHLY, YEARLY
	}

	private Long id;
	private int eventType;
	private String title;
	private String description;
	private Date timeCreated;
	private Date timeFrom;
	private Date timeTo;
	private Date lastAlert;
	private int alertType;
	private int repeatType;
	private Integer lastAlertLon;
	private Integer lastAlertLat;
	private String properties;
	private Integer remoteId;	
	private List<Tag> tags;
	private Location primaryLocation;

	public Event() {
		super();
	}

	public Event(Long id, int version, EventType eventType, String title, String description, Date timeCreated,
			RepeatType repeatType, Date timeFrom, Date timeTo, Date lastAlert, AlertType alertType, Integer lastAlertLon,
			Integer lastAlertLat, String properties, Integer remoteId) {
		this(id, version, eventType.ordinal(), title, description, timeCreated, repeatType.ordinal(), timeFrom,
				timeTo, lastAlert, alertType.ordinal(), lastAlertLon, lastAlertLat, properties, remoteId);
	}

	public Event(Long id, int version, int eventType, String title, String description, Date timeCreated,
			int repeatType, Date timeFrom, Date timeTo, Date lastAlert, int alertType, Integer lastAlertLon,
			Integer lastAlertLat, String properties, Integer remoteId) {
		super();
		this.setVersion(version);	
		
		this.id = id;		
		this.eventType = eventType;
		this.title = title;
		this.description = description;
		this.timeCreated = timeCreated;
		this.repeatType = repeatType;
		this.timeFrom = timeFrom;
		this.timeTo = timeTo;
		this.lastAlert = lastAlert;
		this.alertType = alertType;
		this.lastAlertLon = lastAlertLon;
		this.lastAlertLat = lastAlertLat;
		this.properties = properties;
		this.remoteId = remoteId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getEventType() {
		return eventType;
	}

	@JSONField(defaultValue = "PRIVATE")
	public EventType getEventTypeEnum() {
		return EventType.values()[eventType];
	}

	public void setEventTypeEnum(EventType eventType) {
		this.eventType = eventType.ordinal();
	}

	public void setEventType(int eventType) {
		this.eventType = eventType;
	}

	@JSONField
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@JSONField
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@JSONField
	public Date getTimeCreated() {
		return timeCreated;
	}

	public void setTimeCreated(Date timeCreated) {
		this.timeCreated = timeCreated;
	}

	@JSONField
	public Date getTimeFrom() {
		return timeFrom;
	}

	public void setTimeFrom(Date timeFrom) {
		this.timeFrom = timeFrom;
	}

	@JSONField
	public Date getTimeTo() {
		return timeTo;
	}

	public void setTimeTo(Date timeTo) {
		this.timeTo = timeTo;
	}

	@JSONField
	public Date getLastAlert() {
		return lastAlert;
	}

	public void setLastAlert(Date lastAlert) {
		this.lastAlert = lastAlert;
	}

	public int getAlertType() {
		return alertType;
	}

	public void setAlertType(int alertType) {
		this.alertType = alertType;
	}

	@JSONField(defaultValue = "LOCATION")
	public AlertType getAlertTypeEnum() {
		return AlertType.values()[alertType];
	}

	public void setAlertTypeEnum(AlertType alertType) {
		this.alertType = alertType.ordinal();
	}

	@JSONField
	public Integer getLastAlertLon() {
		return lastAlertLon;
	}

	public void setLastAlertLon(Integer lastAlertLon) {
		this.lastAlertLon = lastAlertLon;
	}

	@JSONField
	public Integer getLastAlertLat() {
		return lastAlertLat;
	}

	public void setLastAlertLat(Integer lastAlertLat) {
		this.lastAlertLat = lastAlertLat;
	}

	@JSONField
	public String getProperties() {
		return properties;
	}

	public void setProperties(String properties) {
		this.properties = properties;
	}

	public int getRepeatType() {
		return repeatType;
	}

	public void setRepeatType(int repeatType) {
		this.repeatType = repeatType;
	}

	@JSONField(defaultValue = "ONCE")
	public RepeatType getRepeatTypeEnum() {
		return RepeatType.values()[repeatType];
	}

	public void setRepeatTypeEnum(RepeatType repeatType) {
		this.repeatType = repeatType.ordinal();
	}

	@JSONField(elementType=Tag.class)
	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	@JSONField
	public Location getLocation() {
		return primaryLocation;
	}
	
	public void setLocation(Location loc) {
		this.primaryLocation = loc;
	}

	@JSONField
	public Integer getRemoteId() {
		return remoteId;
	}

	public void setRemoteId(Integer remoteId) {
		this.remoteId = remoteId;
	}

//	primary location setup
	
	private void checkPrimaryLocation() {
		if (this.primaryLocation == null) {
			this.primaryLocation = new Location();
		}
	}
	
	public void setLocId(Long id) {
		if (id != null) {
			checkPrimaryLocation();
			this.primaryLocation.setId(id);
		}
	}
	
	public void setLocLon(Integer lon) {
		if (lon != null) {
			checkPrimaryLocation();
			if (lon != null) {
				this.primaryLocation.setLon(lon);
			}
		}
	}
	
	public void setLocLat(Integer lat) {
		if (lat != null) {
			checkPrimaryLocation();
			if (lat != null) {
				this.primaryLocation.setLat(lat);
			}
		}
	}
	
	public void setLocRadius(Integer radius) {
		if (radius != null) {
			checkPrimaryLocation();
			if (radius != null) {
				this.primaryLocation.setRadius(radius);
			}
		}
	}
}
